package filter

import (
	"cgs-server/helper"
	"time"
)

type DateRange int

const (
	DefaultDateRange DateRange = iota // (0,"默认"),
	Today                             //(1,"今天"),
	Yesterday                         //(2,"昨天"),
	Tomorrow                          //(3,"明天"),
	ThisWeek                          //(4,"本周"),
	LastWeek                          //(5,"上周"),
	NextWeek                          //(6,"下周"),
	ThisMonth                         //(7,"本月"),
	LastMonth                         //(8,"上月"),
	NextMonth                         //(9,"下月"),
	LastEnum                          //(10,"上.."),
	NextEnum                          //(11,"下.."),
	ThisQuarter                       //(12,"本季度"),
	LastQuarter                       //(13,"上季度"),
	NextQuarter                       //(14,"下季度"),
	ThisYear                          //(15,"本年"),
	LastYear                          //(16,"去年"),
	NextYear                          //(17,"明年"),
	Customize                         //(18,"自定义");
)

const (
	Last7Days DateRange = iota + 21 // 过去 7 天，不包括今天
	Last14Days
	Last30Days
)

const (
	Next7Days DateRange = iota + 31 // 将来 7 天，不包括今天
	Next14Days
	Next30Days
)

var DataRangeMap = map[DateRange]string{
	DefaultDateRange: "默认",
	Today:            "今天",
	Yesterday:        "昨天",
	Tomorrow:         "明天",
	ThisWeek:         "本周",
	LastWeek:         "上周",
	NextWeek:         "下周",
	ThisMonth:        "本月",
	LastMonth:        "上月",
	NextMonth:        "下月",
	LastEnum:         "上..",
	NextEnum:         "下..",
	ThisQuarter:      "本季度",
	LastQuarter:      "上季度",
	NextQuarter:      "下季度",
	ThisYear:         "本年",
	LastYear:         "去年",
	NextYear:         "明年",
	Customize:        "自定义",
}

func (d DateRange) GetDate(f SortInput) (begin, end time.Time) {
	today := helper.GetZeroTime(time.Now())
	begin = today
	end = today

	switch d {
	case DefaultDateRange: // 一个时间范围
		min, err := helper.ParseExcelDateTime(f.MinValue, "YYYY-MM-DD HH:mm:ss")
		if err != nil {
			break
		}
		max, err := helper.ParseExcelDateTime(f.MaxValue, "YYYY-MM-DD HH:mm:ss")
		if err != nil {
			break
		}
		begin = min
		end = max
	case Today:
		begin = today
		end = today.AddDate(0, 0, 1)
	case Yesterday:
		begin = today.AddDate(0, 0, -1)
		end = today
	case Tomorrow:
		begin = today.AddDate(0, 0, 1)
		end = today.AddDate(0, 0, 2)
	case ThisWeek:
		begin = helper.GetMondayOfWeek(today)
		end = begin.AddDate(0, 0, 7)
	case LastWeek:
		begin = helper.GetMondayOfWeek(today).AddDate(0, 0, -7)
		end = helper.GetMondayOfWeek(today)
	case NextWeek:
		begin = helper.GetMondayOfWeek(today).AddDate(0, 0, 7)
		end = helper.GetMondayOfWeek(today).AddDate(0, 0, 14)
	case ThisMonth:
		begin = helper.GetFirstDayOfMonth(today)
		end = begin.AddDate(0, 1, 0)
	case LastMonth:
		begin = helper.GetFirstDayOfMonth(today).AddDate(0, -1, 0)
		end = helper.GetFirstDayOfMonth(today)
	case NextMonth:
		begin = helper.GetFirstDayOfMonth(today).AddDate(0, 1, 0)
		end = begin.AddDate(0, 1, 0)
	case ThisQuarter:
		begin = helper.GetQuarterDay(today)
		end = begin.AddDate(0, 3, 0)
	case LastQuarter:
		begin = helper.GetQuarterDay(today).AddDate(0, -3, 0)
		end = helper.GetQuarterDay(today)
	case NextQuarter:
		begin = helper.GetQuarterDay(today).AddDate(0, 3, 0)
		end = begin.AddDate(0, 3, 0)
	case ThisYear:
		begin = helper.GetFirstDayOfYear(today)
		end = begin.AddDate(1, 0, 0)
	case LastYear:
		begin = helper.GetFirstDayOfYear(today).AddDate(-1, 0, 0)
		end = helper.GetFirstDayOfYear(today)
	case NextYear:
		begin = helper.GetFirstDayOfYear(today).AddDate(1, 0, 0)
		end = begin.AddDate(1, 0, 0)
	case LastEnum:
		// 过去多少天
		duration, ok := f.Value.(float64)
		if !ok {
			break
		}
		d := int(duration)
		begin = today.AddDate(0, 0, -d)
		if len(f.Values) > 0 {
			if f.Values[0] == "today" {
				end = today.AddDate(0, 0, 1)
			}
		} else {
			end = today
		}
	case NextEnum:
		// 将来多少天
		duration, ok := f.Value.(float64)
		if !ok {
			break
		}
		d := int(duration)
		end = today.AddDate(0, 0, d+1)
		if len(f.Values) > 0 {
			if f.Values[0] == "today" {
				begin = today
			}
		} else {
			begin = today.AddDate(0, 0, 1)
		}
	case Customize:
		// 自定义日期 (单位：天)
		t, err := helper.StringToTime(f.Value.(string), "YYYY-MM-DD")
		if err != nil {
			break
		}
		begin = helper.GetZeroTime(t)
		end = begin.AddDate(0, 0, 1)
	case Last7Days:
		begin = today.AddDate(0, 0, -7)
		end = today
	case Last14Days:
		begin = today.AddDate(0, 0, -14)
		end = today
	case Last30Days:
		begin = today.AddDate(0, 0, -30)
		end = today
	case Next7Days:
		begin = today.AddDate(0, 0, 1)
		end = today.AddDate(0, 0, 8)
	case Next14Days:
		begin = today.AddDate(0, 0, 1)
		end = today.AddDate(0, 0, 15)
	case Next30Days:
		begin = today.AddDate(0, 0, 1)
		end = today.AddDate(0, 0, 31)
	}
	return
}
